<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input v-model="query.id" clearable placeholder="输入名称或IP搜索" style="width: 200px" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <rrOperation />
      </div>
    </div>
    <!--表格渲染-->
    <el-table ref="table" v-loading="crud.loading" :data="crud.data" style="width: 100%" @selection-change="crud.selectionChangeHandler">
      <el-table-column type="selection" min-width="5%" />
      <el-table-column prop="app" label="服务名称" min-width="20%" />
      <el-table-column prop="homePageUrl" label="实例地址" min-width="25%" />
      <el-table-column prop="status" label="服务状态" min-width="15%">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status==='UP'">UP</el-tag>
          <el-tag v-else-if="scope.row.status==='OUT_OF_SERVER'" type="warning">OUT_OF_SERVER</el-tag>
          <el-tag v-else-if="scope.row.status==='DOWN'" type="danger">DOWN</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="leaseInfo.registrationTimestamp" label="注册时间" min-width="20%" />
      <el-table-column v-if="checkPer(['admin','serverDeploy:edit','serverDeploy:del'])" style="width: 200px" label="操作" align="center">
        <template slot-scope="scope">
          <euOperation
            :data="scope.row"
            :permission="permission"
          />
        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <pagination />
  </div>
</template>

<script>

import eurekaServer from '@/api/mnt/eureka'
import { testServerConnect } from '@/api/mnt/connect'
import { validateIP } from '@/utils/validate'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import euOperation from '@crud/EU.operation'
import pagination from '@crud/Pagination'

const defaultForm = { id: null, name: null, ip: null, port: 22, account: 'root', password: null }
export default {
  name: 'Server',
  components: { pagination, rrOperation, euOperation },
  cruds() {
    return CRUD({ title: '服务注册', url: 'api/eureka/application', crudMethod: { ...eurekaServer }})
  },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  data() {
    return {
      accountList: [],
      accountMap: {},
      loading: false,
      permission: {
        up: ['admin', 'eureka:up'],
        outOfService: ['admin', 'eureka:outOfService'],
        down: ['admin', 'eureka:down'],
        offLine: ['admin', 'eureka:offLine']
      },
      rules: {
        name: [
          { required: true, message: '请输入名称', trigger: 'blur' }
        ],
        ip: [
          { required: true, message: '请输入IP', trigger: 'blur' },
          { validator: validateIP, trigger: 'change' }
        ],
        port: [
          { required: true, message: '请输入端口', trigger: 'blur', type: 'number' }
        ],
        account: [
          { required: true, message: '请输入账号', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    testConnectServer() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true
          testServerConnect(this.form).then((res) => {
            this.loading = false
            this.$notify({
              title: res ? '连接成功' : '连接失败',
              type: res ? 'success' : 'error',
              duration: 2500
            })
          }).catch(() => {
            this.loading = false
          })
        }
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  ::v-deep .el-input-number .el-input__inner {
    text-align: left;
  }
</style>
